﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Funtion_TT
/// </summary>
public class Funtion_TT
{
    SqlConnection conn;
    public Funtion_TT()
    {
        //
        // TODO: Add constructor logic here
        //
        try
        {
            conn = new SqlConnection();
            conn.ConnectionString = Connection.str;
            conn.Open();
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
        }
        catch (Exception)
        {

        }
    }
    public DataTable getSanPhamNew()
    {
        string sql = "Select top 4 * from Camera   ORDER BY ID_C DESC ";
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataTable dt = new DataTable();
        if (ds.Tables.Count > 0)
        {
            dt = ds.Tables[0];
        }
        conn.Close();
        return dt;
    }
    public DataTable getSanPhamDefault()
    {
        string sql = "Select top 9 * from Camera   ORDER BY ID_C DESC ";
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataTable dt = new DataTable();
        if (ds.Tables.Count > 0)
        {
            dt = ds.Tables[0];
        }
        conn.Close();
        return dt;
    }
    public int CheckAccount(string acc)
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }
        SqlCommand cmd = new SqlCommand("select * from Person where TK_ND='" + acc + "'", conn);
        if (cmd.ExecuteReader().HasRows)
        {
            cmd.Connection.Close();
            return 1;

        }
        else
        {
            cmd.Connection.Close();
            return 0;

        }

    }
    public void AddNewRegister(string tk, string mk, string email, string sdt, string diachi)
    {
        string sql = "insert into Person(TK_ND,MK_ND,Email_ND ,SDT_ND ,Address_ND)" +
        "values (@TK_ND,@MK_ND,@Email_ND,@SDT_ND,@Address_ND)";
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.Add("@TK_ND", tk);
        cmd.Parameters.Add("@MK_ND", mk);
        cmd.Parameters.Add("@Email_ND", email);
        cmd.Parameters.Add("@SDT_ND", sdt);
        cmd.Parameters.Add("@Address_ND", diachi);
        //cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        //cmd.Connection.Close();
    }
    public int CheckLoginNguoiDung(string acc, string pass)
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }
        SqlCommand cmd = new SqlCommand("select * from Person where TK_ND='" + acc + "' and MK_ND='" + pass + "'", conn);
        if (cmd.ExecuteReader().HasRows)
        {
            cmd.Connection.Close();
            return 1;

        }
        else
        {
            cmd.Connection.Close();
            return 0;

        }

    }


}